from typing import Annotated


# Annotated[<类型>, <元数据1>, <元数据2>, ...]
# variable: Annotated[<类型>, <元数据1>, <元数据2>, ...]
# 给一个整数类型附加一个字符串描述
Age = Annotated[int, "This is the age of the user, must be non-negative"]

def get_user_age() -> Age:
    return 25

print(get_user_age())


# 字段约束
from pydantic import BaseModel, Field
from typing import Annotated
from typing_extensions import TypedDict  # 对于 Python < 3.11

# 方法 1: 使用 Annotated (推荐)
class User(BaseModel):
    # 使用 Annotated 将约束和类型绑定
    name: Annotated[str, Field(..., min_length=1, max_length=100)]
    age: Annotated[int, Field(gt=0, le=120, description="Human age, must be between 1 and 120")]
    email: Annotated[str, Field(pattern=r"^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$")]

# 方法 2: 传统默认值写法 (仍然有效，但不那么“纯粹”)
class UserLegacy(BaseModel):
    name: str = Field(..., min_length=1, max_length=100)
    age: int = Field(gt=0, le=120, description="Human age, must be between 1 and 120")

# 实例化与验证
user = User(name="Alice", age=300, email="alice@example.com")
print(user)